<?php
namespace app\dboard\controller;
use think\Controller;
use think\Db;
use services\OssService;
class Index extends Base{
    public $error_data=['ErrorCode'=>0,'ErrorMsg'=>'','ErrorSql'=>'','Data'=>[]];
    public $AccountInfo=[];
    public function initialize(){
        parent::initialize();
        $this->Logic = Controller('index','logic');
    }

    // 吊机员任务
    public function crewman_task()
    {
        if($this->request->isPost()){
            $param = $this->request->param();
            $draw = trim($param['draw']);
            list($total,$filter_count,$data_list)=$this->Logic->crewmanTask($param);
            $return_data = ['draw'=>$draw,'recordsTotal' => $total,'recordsFiltered' =>$filter_count,'data'=>$data_list===null?[]:$data_list];
            return json($return_data)->code(200)->header(['Content-Type' => 'application/json']);
        }
        return view('crewman_task');
    }

    // 装车
    public function load_car()
    {
        if ($this->request->isPost()) {
            $param = $this->request->param();
            $param['id'] = intval($param['id']);
            if (!$param['id']) {
                $this->res_data['ErrorMsg'] = '缺少必要参数';
                return $this->print_result($this->res_data);
            }
            $res = $this->Logic->loadCar($param);
            if ($res['ErrorCode']==0) {
                $this->res_data['ErrorMsg'] = $res['ErrorMsg'];
                return $this->print_result($this->res_data);
            }
            $this->res_data['ErrorMsg'] = $res['ErrorMsg'];
            $this->res_data['ErrorCode'] = 1;
            return $this->print_result($this->res_data);
        }
    }

    // 用户列表
    public function user_list()
    {
        if($this->request->isPost()){
            $param = $this->request->param();
            if (isset($param["filter_param"]['start_time'])) {
                $param["filter_param"]['start_time'] = strtotime($param["filter_param"]['start_time']);
            }
            if (isset($param["filter_param"]['end_time'])) {
                $param["filter_param"]['end_time'] = strtotime($param["filter_param"]['end_time']);
            }
            $draw = trim($param['draw']);
            list($total,$filter_count,$data_list)=$this->Logic->userList($param);
            $return_data = ['draw'=>$draw,'recordsTotal' => $total,'recordsFiltered' =>$filter_count,'data'=>$data_list===null?[]:$data_list];
            return json($return_data)->code(200)->header(['Content-Type' => 'application/json']);
        }
        return view('user_list');
    }

    // 封禁
    public function change_user_status()
    {
        if ($this->request->isPost()) {
            $param = $this->request->param();
            if (!isset($param['id']) || !isset($param['status'])) {
                $this->res_data['ErrorMsg'] = '缺少必要参数';
                return $this->print_result($this->res_data);
            }
            $param['id'] = intval($param['id']);
            $param['status'] = intval($param['status']);
            $res = $this->Logic->changeUserStatus($param);
            if ($res['ErrorCode']==0) {
                $this->res_data['ErrorMsg'] = $res['ErrorMsg'];
                return $this->print_result($this->res_data);
            }
            $this->res_data['ErrorMsg'] = $res['ErrorMsg'];
            $this->res_data['ErrorCode'] = 1;
            return $this->print_result($this->res_data);
        }
    }

    // 货运需求管理
    public function freight_order_task()
    {
        if($this->request->isPost()){
            $param = $this->request->param();
            if (isset($param["filter_param"]['start_time'])) {
                $param["filter_param"]['start_time'] = strtotime($param["filter_param"]['start_time']);
            }
            if (isset($param["filter_param"]['end_time'])) {
                $param["filter_param"]['end_time'] = strtotime($param["filter_param"]['end_time']);
            }
            $draw = trim($param['draw']);
            list($total,$filter_count,$data_list)=$this->Logic->freightOrderTask($param);
            $return_data = ['draw'=>$draw,'recordsTotal' => $total,'recordsFiltered' =>$filter_count,'data'=>$data_list===null?[]:$data_list];
            return json($return_data)->code(200)->header(['Content-Type' => 'application/json']);
        }
        return view('freight_order_task');
    }

    public function change_status()
    {
        if ($this->request->isPost()) {
            $param = $this->request->param();
            if (!isset($param['id']) || !isset($param['status'])) {
                $this->res_data['ErrorMsg'] = '缺少必要参数';
                return $this->print_result($this->res_data);
            }
            $param['id'] = intval($param['id']);
            $param['status'] = intval($param['status']);
            $res = $this->Logic->changeStatus($param);
            if ($res['ErrorCode']==0) {
                $this->res_data['ErrorMsg'] = $res['ErrorMsg'];
                return $this->print_result($this->res_data);
            }
            $this->res_data['ErrorMsg'] = $res['ErrorMsg'];
            $this->res_data['ErrorCode'] = 1;
            return $this->print_result($this->res_data);
        }
    }

    // 货运需求历史记录
    public function freight_order_list()
    {
        if($this->request->isPost()){
            $param = $this->request->param();
            $draw = trim($param['draw']);
            list($total,$filter_count,$data_list)=$this->Logic->freightOrderTask($param,1);
            $return_data = ['draw'=>$draw,'recordsTotal' => $total,'recordsFiltered' =>$filter_count,'data'=>$data_list===null?[]:$data_list];
            return json($return_data)->code(200)->header(['Content-Type' => 'application/json']);
        }
        return view('freight_order_list');
    }

    // 物品类型列表
    public function item_type_list()
    {
        if($this->request->isPost()){
            $param = $this->request->param();
            $draw = trim($param['draw']);
            list($total,$filter_count,$data_list)=$this->Logic->itemTypeList($param);
            $return_data = ['draw'=>$draw,'recordsTotal' => $total,'recordsFiltered' =>$filter_count,'data'=>$data_list===null?[]:$data_list];
            return json($return_data)->code(200)->header(['Content-Type' => 'application/json']);
        }
        return view('item_type_list');
    }

    // 获取物品类型
    public function get_item_type()
    {
        if ($this->request->isPost()) {
            $id = $this->request->param('id');
            if (!$id) {
                $this->res_data['ErrorMsg'] = '缺少必要参数';
                return $this->print_result($this->res_data);
            }
            $this->res_data['Data'] = Db::name('item_type')->where('id',$id)->find();
            $this->res_data['ErrorMsg'] = 'success';
            $this->res_data['ErrorCode'] = 1;
            return $this->print_result($this->res_data);
        }
    }

    // 物品类型
    public function add_edit_item_type()
    {
        if ($this->request->isPost()) {
            $param = $this->request->param();
            if (isset($param['id'])) {
                $param['id'] = intval($param['id']);
            }
            if (!isset($param['name'])) {
                $this->res_data['ErrorMsg'] = '请填写物品名称';
                return $this->print_result($this->res_data);
            }
            $param['name'] = htmlspecialchars($param['name']);
            $res = $this->Logic->addEditItemType($param);
            if ($res['ErrorCode']==0) {
                $this->res_data['ErrorMsg'] = $res['ErrorMsg'];
                return $this->print_result($this->res_data);
            }
            $this->res_data['ErrorMsg'] = $res['ErrorMsg'];
            $this->res_data['ErrorCode'] = 1;
            return $this->print_result($this->res_data);
        }
    }

    // 箱柜尺寸列表
    public function size_list()
    {
        if($this->request->isPost()){
            $param = $this->request->param();
            $draw = trim($param['draw']);
            list($total,$filter_count,$data_list)=$this->Logic->sizeList($param);
            $return_data = ['draw'=>$draw,'recordsTotal' => $total,'recordsFiltered' =>$filter_count,'data'=>$data_list===null?[]:$data_list];
            return json($return_data)->code(200)->header(['Content-Type' => 'application/json']);
        }
        return view('size_list');
    }

    // 获取尺寸
    public function get_size()
    {
        if ($this->request->isPost()) {
            $id = $this->request->param('id');
            if (!$id) {
                $this->res_data['ErrorMsg'] = '缺少必要参数';
                return $this->print_result($this->res_data);
            }
            $this->res_data['Data'] = Db::name('size')->where('id',$id)->find();
            $this->res_data['ErrorMsg'] = 'success';
            $this->res_data['ErrorCode'] = 1;
            return $this->print_result($this->res_data);
        }
    }

    // 箱柜尺寸
    public function add_edit_size()
    {
        if ($this->request->isPost()) {
            $param = $this->request->param();
            if (isset($param['id'])) {
                $param['id'] = intval($param['id']);
            }
            if (!isset($param['size'])) {
                $this->res_data['ErrorMsg'] = '请填写箱柜尺寸';
                return $this->print_result($this->res_data);
            }
            $param['size'] = htmlspecialchars($param['size']);
            $res = $this->Logic->addEditSize($param);
            if ($res['ErrorCode']==0) {
                $this->res_data['ErrorMsg'] = $res['ErrorMsg'];
                return $this->print_result($this->res_data);
            }
            $this->res_data['ErrorMsg'] = $res['ErrorMsg'];
            $this->res_data['ErrorCode'] = 1;
            return $this->print_result($this->res_data);
        }
    }

    // banner列表
    public function banner_list()
    {
        if($this->request->isPost()){
            $param = $this->request->param();
            $draw = trim($param['draw']);
            list($total,$filter_count,$data_list)=$this->Logic->bannerList($param);
            $return_data = ['draw'=>$draw,'recordsTotal' => $total,'recordsFiltered' =>$filter_count,'data'=>$data_list===null?[]:$data_list];
            return json($return_data)->code(200)->header(['Content-Type' => 'application/json']);
        }
        return view('banner_list');
    }

    // 获取banner
    public function get_banner()
    {
        if ($this->request->isPost()) {
            $id = $this->request->param('id');
            if (!$id) {
                $this->res_data['ErrorMsg'] = '缺少必要参数';
                return $this->print_result($this->res_data);
            }
            $banner = Db::name('banner')->where('id',$id)->find();
            if ($banner) {
                $banner['photo'] = get_newpic_url($banner['photo']);
            }
            $this->res_data['Data'] = $banner;
            $this->res_data['ErrorMsg'] = 'success';
            $this->res_data['ErrorCode'] = 1;
            return $this->print_result($this->res_data);
        }
    }

    // banner
    public function add_edit_banner()
    {
        if ($this->request->isPost()) {
            $param = $this->request->param();
            if (isset($param['title'])) {
                $param['title'] = htmlspecialchars($param['title']);
            } else {
                $param['title'] = '';
            }
            if (isset($param['link'])) {
                $param['link'] = htmlspecialchars($param['link']);
                if (!filter_var($param['link'], FILTER_VALIDATE_URL)) {
                    $this->res_data['ErrorMsg'] = '链接格式错误';
                    return $this->print_result($this->res_data);
                }
            } else {
                $param['link'] = '';
            }
            if (!isset($param['photo']) || !$param['photo']) {
                $this->res_data['ErrorMsg'] = '请选择图片';
                return $this->print_result($this->res_data);
            }
            $base64_image = str_replace(' ', '+', $param['photo']); 
            if (!preg_match('/^(data:\s*image\/(\w+);base64,)/', $base64_image, $result)) {
                $this->res_data['ErrorMsg'] = '上传图片失败';
                return $this->print_result($this->res_data);
            }
            //所有图片后缀改成jpg  
            if ($result[2] != 'jpg') {  
                $image_name = uniqid() . '.jpg';  
            }  
            $image_file = "51xcx/" . $image_name; 
            $OssService = new OssService();
            $file_content = base64_decode(str_replace($result[1], '', $base64_image));
            $oss_res = $OssService->upload_file_content($file_content, $image_file);
            if (!$oss_res) {
                $this->res_data['ErrorMsg'] = '上传图片失败';
                return $this->print_result($this->res_data);
            }
            $param['photo'] = "51xcx/" . $image_name; 
            $res = $this->Logic->addEditBanner($param);
            if ($res['ErrorCode']==0) {
                $this->res_data['ErrorMsg'] = $res['ErrorMsg'];
                return $this->print_result($this->res_data);
            }
            $this->res_data['ErrorMsg'] = $res['ErrorMsg'];
            $this->res_data['ErrorCode'] = 1;
            return $this->print_result($this->res_data);
        }
    }

    // 关于我们
    public function about_us()
    {
        if ($this->request->isPost()) {
            $param = $this->request->param();
            if (!isset($param['content'])) {
                $this->res_data['ErrorMsg'] = '请填写内容';
                return $this->print_result($this->res_data);
            }
            $param['content'] = html_entity_decode($param['content']);
            $update = Db::name('about_us')->where('id',1)->update(['content'=>$param['content'],'update_time'=>now_time()]);
            if (!$update) {
                $this->res_data['ErrorMsg'] = $res['ErrorMsg'];
                return $this->print_result($this->res_data);
            }
            $this->res_data['Data'] = Db::name('about_us')->where('id',1)->value('content');
            $this->res_data['ErrorMsg'] = 'success';
            $this->res_data['ErrorCode'] = 1;
            return $this->print_result($this->res_data);
        }
        return view('about_us');
    }

    // 获取关于我们
    public function get_about_us()
    {
        if ($this->request->isPost()) {
            $about_us = Db::name('about_us')->where('id',1)->find();
            $this->res_data['Data'] = $about_us;
            $this->res_data['ErrorMsg'] = 'success';
            $this->res_data['ErrorCode'] = 1;
            return $this->print_result($this->res_data);
        }
    }

}
